//
//  apple1
//
//  Created by apple on 2021/11/10.
//  Copyright © 2021 zglee. All rights reserved.
//mj总胆固醇(检测结果)

#import "MJTotalDetectionResultVC.h"
#import "MJDetectionResultInfoTbvCell.h"
#import "MJCommomResultICell.h"
#import "MemberInfoVC.h"
#import "MJUMITbvFooterView.h"
#import "PrinterSrarchView.h"

@interface MJTotalDetectionResultVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) MJUITableView *tableView;
@property (nonatomic, strong) MJNavBarView *navBarView;
@property (nonatomic, strong) MJTotalCholesterolModel *model;
@property (nonatomic, strong) MJUIGradientView *gradientView;
@property (nonatomic, strong) MJUMITbvFooterView *tbvFootView;
@end

@implementation MJTotalDetectionResultVC

- (instancetype)initWithModel:(MJTotalCholesterolModel *)model{
    if (self = [super init]) {
        self.model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)mj_setUI{
    [super mj_setUI];
    
    self.view.backgroundColor = UIColor.mj_backgroundColor;
    [self.view addSubview:self.gradientView];
    [self.view addSubview:self.navBarView];
    [self.view addSubview:self.tableView];
    [self.navBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(STATUS_BAR_HEIGHT);
        make.height.equalTo(@(NAVBAR_HEIGHT));
        make.left.right.equalTo(self.view);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navBarView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    [self.navigationController setNavigationBarHidden:true animated:NO];
}

#pragma mark - 方法

#pragma mark - 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MJWeakSelf
    if(indexPath.row == 0){
        MJDetectionResultInfoTbvCell *cell = [MJDetectionResultInfoTbvCell reuseForTableView:tableView forIndexPath:indexPath];
        cell.recordDataBaseModel = self.model;
        [cell.infoButton mj_addAction:^(UIButton *button) {
            [HealthHelper jumpMemberInfoViewController:weakSelf patientId:weakSelf.model.patientId];
        }];
        return cell;
    }else{
        MJCommomResultICell *cell = [MJCommomResultICell reuseForTableView:tableView forIndexPath:indexPath];
        cell.isFromDetection = self.isFromDetection;
        cell.totalCholesterolModel = self.model;
        
        cell.rightTitleLabel.text = [self.model.unit isEqualToString:unitmgdL] ? unitmmolL:unitmgdL;
        [cell.rightImvTitleButton mj_addAction:^(UIButton *button) {
            [weakSelf.model switchUnit];
            [weakSelf.tableView reloadData];
        }];
        return cell;
    }
}

#pragma mark - 懒加载
- (MJUITableView *)tableView{
    if (!_tableView) {
        MJUITableView *tableView = [[MJUITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorInset= UIEdgeInsetsMake(0,0, 0, 0);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.backgroundColor = UIColor.clearColor;
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.estimatedRowHeight = 140;
        [MJDetectionResultInfoTbvCell registerNibWithTableView:tableView];
        [MJCommomResultICell registerNibWithTableView:tableView];
        tableView.tableFooterView = self.tbvFootView;

        _tableView = tableView;
    }
    return _tableView;
}

- (MJNavBarView *)navBarView {
    if (!_navBarView) {
        MJWeakSelf
        _navBarView = [MJNavBarView mj_loadNibWithOwner:self];
        [_navBarView.btnBack mj_addAction:^(UIButton *btn) {
            [weakSelf mj_popViewController];
        }];
        _navBarView.rightImvTitleView.hidden = false;
        _navBarView.rightTitleLabel.hidden = true;
        _navBarView.rightTitleImageView.image = MJUIImageMake(@"icon_print");
        [_navBarView.rightImvTitleButton mj_addAction:^(UIButton *button) {
            //
            PrinterSrarchView *printerSrarchView = [[PrinterSrarchView alloc] init];
            printerSrarchView.totalCholesterolModel = weakSelf.model;
            printerSrarchView.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            weakSelf.modalPresentationStyle = UIModalPresentationCurrentContext;
            [weakSelf.navigationController presentViewController:printerSrarchView animated:NO completion:nil];
        }];
//        _navBarView.rightImvTitleView.hidden = false;
//        _navBarView.rightTitleLabel.text = [weakSelf.model.unit isEqualToString:unitmgdL] ? unitmmolL:unitmgdL;
//        [_navBarView.rightImvTitleButton mj_addAction:^(UIButton *button) {
//            [weakSelf.model switchUnit];
//            weakSelf.navBarView.rightTitleLabel.text = [weakSelf.model.unit isEqualToString:unitmgdL] ? unitmmolL:unitmgdL;
//            [weakSelf.tableView reloadData];
//        }];
        _navBarView.labTitle.mj_localizable = [HealthHelper detectionTitle:DetectionItemTypeTC];
    }
    return _navBarView;
}

- (MJUIGradientView *)gradientView {
    if (!_gradientView) {
        MJUIGradientView *view = [[MJUIGradientView alloc] init];
        view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 200);
        _gradientView = view;
    }
    return _gradientView;
}
- (MJUMITbvFooterView *)tbvFootView {
    if (!_tbvFootView) {
        _tbvFootView = [MJUMITbvFooterView mj_loadNibWithOwner:self];
        _tbvFootView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100);
        _tbvFootView.autoresizingMask = UIViewAutoresizingNone;
    }
    return _tbvFootView;
}
@end
 
